//
//  RJBarView.m
//  AirQuality
//
//  Created by Po on 15/4/2.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJBarView.h"
#import "NewAQIBar.h"
@interface RJBarView ()

@property (retain, nonatomic) NSDictionary * data;
@property (strong, nonatomic) UILabel * pmLable;
- (void)initInterface;
@end

@implementation RJBarView

- (instancetype)initWithFrame:(CGRect)frame data:(NSDictionary *)data
{
    self = [super initWithFrame:frame];
    if (self) {
        _data = [NSDictionary dictionaryWithDictionary:data];
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    
//    NSArray * titleArray = @[@"PM2_5",@"PM10",@"O3",@"SO2",@"NO2",@"CO"];
//    NSArray * showTittleArray = @[@"PM",@"PM₁₀",@"O₃", @"SO₂",@"NO₂",@"CO"];
    NSArray * titleArray = @[@"ISO2",@"INO2",@"ICO",@"IO3",@"IPM10",@"IPM2_5"];
    NSArray * showTittleArray = @[@"SO₂",@"NO₂",@"CO", @"O₃",@"PM₁₀",@"PM"];
    NSArray * colors = @[
                         GetRGBColor(0, 228, 0, 1),
                         GetRGBColor(235, 234, 20, 1),
                         GetRGBColor(255, 126, 0, 1),
                         GetRGBColor(255, 0, 0, 1),
                         GetRGBColor(153, 0, 76, 1),
                         GetRGBColor(126, 0, 35, 1),
                         GetRGBColor(164, 164, 166, 1)                         ];
    NSArray * trackcolors = @[
                         GetRGBColor(0, 228, 0, 0.3),
                         GetRGBColor(255, 234, 20, 0.3),
                         GetRGBColor(255, 126, 0, 0.3),
                         GetRGBColor(255, 0, 0, 0.3),
                         GetRGBColor(153, 0, 76, 0.3),
                         GetRGBColor(126, 0, 35, 0.3),
                         GetRGBColor(164, 164, 166, 0.3)
                         ];
    CGFloat width = ScreenWidth / 3;
    
    for (NSUInteger i = 0; i < titleArray.count; i ++) {
        NSNumber * dataNum = [_data count] != 0 ? _data[titleArray[i]] : @0;
        NSUInteger rank = [RJTools getRankWithNum:[dataNum floatValue]];
        UIColor * color = colors[rank];
        NewAQIBar * aqiBar = [[NewAQIBar alloc] initWithFrame:CGRectMake(15 + width * (i % 3), 55 * (i / 3), width - 30, 30)];
        [self addSubview:aqiBar];
        if ([showTittleArray[i] isEqualToString:@"PM"]) {
            CGRect rect = aqiBar.typeLabel.frame;
            CGFloat buttonWidth = rect.size.width;
            CGFloat height = rect.size.height;
            _pmLable  = [[UILabel alloc] init];
            _pmLable.text = @"2.5";
            _pmLable.frame = CGRectMake(buttonWidth-17, height/6-1, 20, 20);
            _pmLable.font = [UIFont systemFontOfSize:9];
            [aqiBar.typeLabel addSubview:_pmLable];
        }
        NSArray * arr = [RJTools returnRankWithType:titleArray[i] num:[dataNum floatValue]];
        aqiBar.progress.progress = [arr[1] floatValue];
        aqiBar.progress.progressTintColor = colors[rank];
        aqiBar.progress.trackTintColor = trackcolors[rank];
        [aqiBar.typeLabel setText:showTittleArray[i]];
        [aqiBar.typeLabel setFont:[UIFont systemFontOfSize:14]];
        [aqiBar.numLabel setText:GetStringWithNum(dataNum)];
        NSString * string = GetStringWithNum(dataNum);
        if ([string isEqualToString:@"(null)"] || [string isEqualToString:@"—"] ||[string isEqualToString:@""]) {
            [aqiBar.numLabel setText:@"—"];
        }
        [aqiBar.numLabel setFont:[UIFont systemFontOfSize:16]];
        [aqiBar.numLabel setTextColor:color];
        [aqiBar.colorLine setBackgroundColor:color];
//        [aqiBar.detailLabel setText:(i == 5) ? @"mg/m³" : @"ug/m³"];
    }
}

- (NewAQIBar *)getAQIBarWithFrame:(CGRect)frame Data:(NSDictionary *)data
{
    NewAQIBar * aqiBar = [[NewAQIBar alloc] initWithFrame:frame];
    
    return aqiBar;
}

@end
